﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using BrainTechLLC;
using ImageAppViewModels;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for ThumbViewCommandBar.xaml
    /// </summary>
    public partial class ThumbViewCommandBar : UserControl
    {
        public event EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>> RequestDelete;
        public event EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>> RequestResizeDelete;
        public event EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>> RequestResize;
        public event EventHandler<ObjectEventArgsNonRef<RotationParams, bool>> RequestRotate;

        public static readonly DependencyProperty FileImageViewModelProperty =
            DependencyProperty.Register("FileImageViewModel", typeof(FileImageViewModel), typeof(ThumbViewCommandBar), new PropertyMetadata(null, OnFileImageViewModelPropertyChanged));

        public FileImageViewModel FileImageViewModel
        {
            get { return (FileImageViewModel)GetValue(FileImageViewModelProperty); }
            set { SetValue(FileImageViewModelProperty, value); }
        }

        private static void OnFileImageViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ThumbViewCommandBar source = d as ThumbViewCommandBar;
            source.UpdateFileImageViewModel(e.OldValue as FileImageViewModel, e.NewValue as FileImageViewModel);
        }

        public static readonly DependencyProperty SingleDeleteOnlyProperty =
            DependencyProperty.Register("SingleDeleteOnly", typeof(bool), typeof(ThumbViewCommandBar), new PropertyMetadata(false));

        public bool SingleDeleteOnly
        {
            get { return (bool)GetValue(SingleDeleteOnlyProperty); }
            set { SetValue(SingleDeleteOnlyProperty, value); }
        }
        
        private void UpdateFileImageViewModel(FileImageViewModel viewModelOld, FileImageViewModel viewModelNew)
        {
            stack2.DataContext = viewModelNew;
        }

        public static readonly DependencyProperty ShowViewerTopmostButtonProperty =
            DependencyProperty.Register("ShowViewerTopmostButton", typeof(bool), typeof(ThumbViewCommandBar), new PropertyMetadata(false, OnShowViewerTopmostButtonPropertyChanged));

        public bool ShowViewerTopmostButton
        {
            get { return (bool)GetValue(ShowViewerTopmostButtonProperty); }
            set { SetValue(ShowViewerTopmostButtonProperty, value); }
        }

        private static void OnShowViewerTopmostButtonPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ThumbViewCommandBar source = d as ThumbViewCommandBar;
            source.UpdateShowViewerTopmostButton((bool)e.OldValue, (bool)e.NewValue);
        }

        private void UpdateShowViewerTopmostButton(bool oldValue, bool newValue)
        {
            btnViewerTopmost.Visibility = newValue ? Visibility.Visible : Visibility.Collapsed;
        }

        public ThumbViewCommandBar()
        {
            InitializeComponent();
            stack2.DataContext = null;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            var vm = (sender as Button).Tag as FileImageViewModel;
            RequestDelete.RaiseEvent(this, vm, SingleDeleteOnly);
        }

        private void btnResize_Click(object sender, RoutedEventArgs e)
        {
            var vm = (sender as Button).Tag as FileImageViewModel;
            RequestResize.RaiseEvent(this, vm, SingleDeleteOnly);            
        }

        private void btnResizeDelete_Click(object sender, RoutedEventArgs e)
        {
            var vm = (sender as Button).Tag as FileImageViewModel;
            RequestResizeDelete.RaiseEvent(this, vm, SingleDeleteOnly);            
        }

        private void btnRotateCounterClockwise_Click(object sender, RoutedEventArgs e)
        {
            var vm = (sender as Button).Tag as FileImageViewModel;
            RequestRotate.RaiseEvent(this, new RotationParams(vm, false), SingleDeleteOnly);            
        }

        private void btnRotateClockwise_Click(object sender, RoutedEventArgs e)
        {
            var vm = (sender as Button).Tag as FileImageViewModel;
            RequestRotate.RaiseEvent(this, new RotationParams(vm, true), SingleDeleteOnly);            
        }

        private void btnCopyToSpecialFolder_Click(object sender, RoutedEventArgs e)
        {
            var vm = (sender as Button).Tag as FileImageViewModel;
            Window1.MainWindow.SpecialFolderHandler.SaveToSpecialFolder(vm, null);
        }
    }
}
